package com.github.weixin.demo.handler;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.github.weixin.demo.persistence.dao.DeviceEventDao;
import com.github.weixin.demo.persistence.entity.DeviceEventEntity;

import cn.timeoverflow.bean.DeviceEvent;

@Component
public class DeviceEventHandler {
	@Autowired DeviceEventDao ded;
	public Object handle(DeviceEvent f){
		if(f.getMsg_type().toLowerCase().equals("bind")){
			//存入数据库
			DeviceEventEntity dee = new DeviceEventEntity();
			BeanUtils.copyProperties(f, dee);
			return ded.save(dee);
		}else{
			//从数据库中删除
			String id = f.getDevice_id();
			DeviceEventEntity dee = ded.findByDeviceId(id);
			if(dee == null || dee.getDevice_id() == null ){
				return "";
			}
			ded.delete(dee);
			return "";
		}
	}
}
